package com.h3c.ptability.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.h3c.ptability.entity.PtNoticeRecord;
import com.h3c.ptability.mapper.PtNoticeRecordMapper;
import com.h3c.ptability.service.CommService;
import com.h3c.ptability.service.IPtNoticeRecordService;
import com.h3c.ptability.dto.PtNoticeRecordDTO;
import com.h3c.ptability.utils.AppBusinessException;
import com.h3c.ptability.utils.InitExampleOrderByUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: PtNoticeRecordServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_notice_record serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
public class PtNoticeRecordServiceImpl extends ServiceImpl<PtNoticeRecordMapper, PtNoticeRecord> implements IPtNoticeRecordService {

    @Resource
    private PtNoticeRecordMapper ptNoticeRecordMapper;
    @Autowired
    CommService commService;
    

    /**
     * @title: selectFuzzyByDto
     * @description: 模糊查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<PtNoticeRecordDTO> selectFuzzyByDto(PtNoticeRecordDTO dto) {
        //List<PtNoticeRecord> list = ptNoticeRecordMapper.selectByExample(initFuzzyExample(dto));
        List<PtNoticeRecord> list = ptNoticeRecordMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto),PtNoticeRecord.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), PtNoticeRecordDTO.class);
    }

    /**
     * @title: selectByDto
     * @description: 精确查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<PtNoticeRecordDTO> selectByDto(PtNoticeRecordDTO dto) {
        //List<PtNoticeRecord> list = ptNoticeRecordMapper.selectByExample(initExample(dto));
        List<PtNoticeRecord> list = ptNoticeRecordMapper.queryList(JSONObject.parseObject(JSONObject.toJSONString(dto),PtNoticeRecord.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), PtNoticeRecordDTO.class);
    }

    /**
     * @title: insertDto
     * @description: 插入操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer insertDto(PtNoticeRecordDTO dto) {
        //if (null == dto.getId() || "".equals(dto.getId())) {
        //    String pkId = idWorker.nextId() + "";
        //    dto.setId(pkId);
        //}
        return ptNoticeRecordMapper.insertPtNoticeRecord(JSONObject.parseObject(JSON.toJSONString(dto), PtNoticeRecord.class));
    }

    /**
     * @title: updateDto
     * @description: 修改操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer updateDto(PtNoticeRecordDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return ptNoticeRecordMapper.updatePtNoticeRecord(JSONObject.parseObject(JSON.toJSONString(dto), PtNoticeRecord.class));
            //return ptNoticeRecordMapper.updateByPrimaryKey(JSONObject.parseObject(JSON.toJSONString(dto), PtNoticeRecord.class));
        //} else {
        //    return 0;
        //}
    }

    /**
     * @title: deleteDto
     * @description: 删除操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer deleteDto(PtNoticeRecordDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return ptNoticeRecordMapper.deletePtNoticeRecord(JSONObject.parseObject(JSON.toJSONString(dto), PtNoticeRecord.class));
            //return ptNoticeRecordMapper.deleteByPrimaryKey(dto.getId());
        //} else {
        //    return 0;
        //}
    }


    /**
     * @title: initFuzzyExample
     * @description: 初始化 模糊查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    private Example initFuzzyExample(PtNoticeRecordDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(PtNoticeRecord.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getNoticeId() && !"".equals(dto.getNoticeId())) {
                criteriaName.andLike("NoticeId", "%" + dto.getNoticeId() + "%");
            }
            if (null != dto.getAppId() && !"".equals(dto.getAppId())) {
                criteriaName.andLike("AppId", "%" + dto.getAppId() + "%");
            }
            if (null != dto.getOrderId() && !"".equals(dto.getOrderId())) {
                criteriaName.andLike("OrderId", "%" + dto.getOrderId() + "%");
            }
            if (null != dto.getApplyUserId() && !"".equals(dto.getApplyUserId())) {
                criteriaName.andLike("ApplyUserId", "%" + dto.getApplyUserId() + "%");
            }
            if (null != dto.getNoticeType() && !"".equals(dto.getNoticeType())) {
                criteriaName.andLike("NoticeType", "%" + dto.getNoticeType() + "%");
            }
            if (null != dto.getTargetId() && !"".equals(dto.getTargetId())) {
                criteriaName.andLike("TargetId", "%" + dto.getTargetId() + "%");
            }
            if (null != dto.getTargetName() && !"".equals(dto.getTargetName())) {
                criteriaName.andLike("TargetName", "%" + dto.getTargetName() + "%");
            }
            if (null != dto.getTargetEmail() && !"".equals(dto.getTargetEmail())) {
                criteriaName.andLike("TargetEmail", "%" + dto.getTargetEmail() + "%");
            }
            if (null != dto.getTargetPhone() && !"".equals(dto.getTargetPhone())) {
                criteriaName.andLike("TargetPhone", "%" + dto.getTargetPhone() + "%");
            }
            if (null != dto.getNoticeTitle() && !"".equals(dto.getNoticeTitle())) {
                criteriaName.andLike("NoticeTitle", "%" + dto.getNoticeTitle() + "%");
            }
            if (null != dto.getNoticeContent() && !"".equals(dto.getNoticeContent())) {
                criteriaName.andLike("NoticeContent", "%" + dto.getNoticeContent() + "%");
            }
            if (null != dto.getCreateTime() && !"".equals(dto.getCreateTime())) {
                criteriaName.andLike("CreateTime", "%" + dto.getCreateTime() + "%");
            }
            if (null != dto.getSendFlag() && !"".equals(dto.getSendFlag())) {
                criteriaName.andLike("SendFlag", "%" + dto.getSendFlag() + "%");
            }
            if (null != dto.getSendTime() && !"".equals(dto.getSendTime())) {
                criteriaName.andLike("SendTime", "%" + dto.getSendTime() + "%");
            }
            if (null != dto.getUploadBusiId() && !"".equals(dto.getUploadBusiId())) {
                criteriaName.andLike("UploadBusiId", "%" + dto.getUploadBusiId() + "%");
            }
            if (null != dto.getNoticeContentPic() && !"".equals(dto.getNoticeContentPic())) {
                criteriaName.andLike("NoticeContentPic", "%" + dto.getNoticeContentPic() + "%");
            }
            if (null != dto.getCopyToEmail() && !"".equals(dto.getCopyToEmail())) {
                criteriaName.andLike("CopyToEmail", "%" + dto.getCopyToEmail() + "%");
            }
        }
        return example;
    }

    /**
     * @title: initExample
     * @description: 初始化 精确查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    private Example initExample(PtNoticeRecordDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(PtNoticeRecord.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getNoticeId() && !"".equals(dto.getNoticeId())) {
                criteriaName.andEqualTo("NoticeId", dto.getNoticeId());
            }
            if (null != dto.getAppId() && !"".equals(dto.getAppId())) {
                criteriaName.andEqualTo("AppId", dto.getAppId());
            }
            if (null != dto.getOrderId() && !"".equals(dto.getOrderId())) {
                criteriaName.andEqualTo("OrderId", dto.getOrderId());
            }
            if (null != dto.getApplyUserId() && !"".equals(dto.getApplyUserId())) {
                criteriaName.andEqualTo("ApplyUserId", dto.getApplyUserId());
            }
            if (null != dto.getNoticeType() && !"".equals(dto.getNoticeType())) {
                criteriaName.andEqualTo("NoticeType", dto.getNoticeType());
            }
            if (null != dto.getTargetId() && !"".equals(dto.getTargetId())) {
                criteriaName.andEqualTo("TargetId", dto.getTargetId());
            }
            if (null != dto.getTargetName() && !"".equals(dto.getTargetName())) {
                criteriaName.andEqualTo("TargetName", dto.getTargetName());
            }
            if (null != dto.getTargetEmail() && !"".equals(dto.getTargetEmail())) {
                criteriaName.andEqualTo("TargetEmail", dto.getTargetEmail());
            }
            if (null != dto.getTargetPhone() && !"".equals(dto.getTargetPhone())) {
                criteriaName.andEqualTo("TargetPhone", dto.getTargetPhone());
            }
            if (null != dto.getNoticeTitle() && !"".equals(dto.getNoticeTitle())) {
                criteriaName.andEqualTo("NoticeTitle", dto.getNoticeTitle());
            }
            if (null != dto.getNoticeContent() && !"".equals(dto.getNoticeContent())) {
                criteriaName.andEqualTo("NoticeContent", dto.getNoticeContent());
            }
            if (null != dto.getCreateTime() && !"".equals(dto.getCreateTime())) {
                criteriaName.andEqualTo("CreateTime", dto.getCreateTime());
            }
            if (null != dto.getSendFlag() && !"".equals(dto.getSendFlag())) {
                criteriaName.andEqualTo("SendFlag", dto.getSendFlag());
            }
            if (null != dto.getSendTime() && !"".equals(dto.getSendTime())) {
                criteriaName.andEqualTo("SendTime", dto.getSendTime());
            }
            if (null != dto.getUploadBusiId() && !"".equals(dto.getUploadBusiId())) {
                criteriaName.andEqualTo("UploadBusiId", dto.getUploadBusiId());
            }
            if (null != dto.getNoticeContentPic() && !"".equals(dto.getNoticeContentPic())) {
                criteriaName.andEqualTo("NoticeContentPic", dto.getNoticeContentPic());
            }
            if (null != dto.getCopyToEmail() && !"".equals(dto.getCopyToEmail())) {
                criteriaName.andEqualTo("CopyToEmail", dto.getCopyToEmail());
            }
        }
        return example;
    }

    @Override
    public void resend(PtNoticeRecordDTO paramsDTO) {
        String noticeId = paramsDTO.getNoticeId();
        PtNoticeRecord record = ptNoticeRecordMapper.selectById(noticeId);
        if (record == null) {
            throw new AppBusinessException("系统异常");
        }
        record.setNoticeId(IdWorker.nextId()+"");
        commService.sendMessage(record);
    }
}
